//
//  DailyProjectViewController.m
//  MagictoLane
//
//  Created by 锋 on 2017/12/28.
//  Copyright © 2017年 HUAYU. All rights reserved.
//

#import "DailyProjectViewController.h"
#import "projectModel.h"
#import "DailyDetailsViewController.h"

static NSString * CellPath = @"dailyCell";
@interface DailyProjectViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *dailyProjectTableView;
@property (nonatomic, assign)NSInteger page;
@property (nonatomic, strong)NSMutableArray *dailyAry;
@end

@implementation DailyProjectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _page = 0;
    _dailyAry = [NSMutableArray arrayWithCapacity:0];
    [self setupUI];
    [self setupDailyProjectTableView];
    [self requestWorkerDailyList];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    _page = 0;
    [self requestWorkerDailyList];
}

- (void)setupUI {
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"项目日报";
    self.view.backgroundColor = RGB(242, 242, 242);
}
- (void)setupDailyProjectTableView {
    _dailyProjectTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - STA_NAV_HEIGHT - SCREER_HeiX) style:UITableViewStyleGrouped];
    _dailyProjectTableView.delegate = self;
    _dailyProjectTableView.dataSource = self;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.000001)];
    view.backgroundColor = [UIColor whiteColor];
    _dailyProjectTableView.tableHeaderView = view;
    _dailyProjectTableView.tableFooterView = view;
    _dailyProjectTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewHeaderData)];
    _dailyProjectTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreFootData)];
    _dailyProjectTableView.mj_footer.automaticallyHidden = YES;
    _dailyProjectTableView.estimatedRowHeight = 0;
    _dailyProjectTableView.estimatedSectionHeaderHeight = 0;
    _dailyProjectTableView.estimatedSectionFooterHeight = 0;
    _dailyProjectTableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_dailyProjectTableView];
}

- (void)loadNewHeaderData
{
    _page = 0;
    [self requestWorkerDailyList];
}

- (void)loadMoreFootData
{
    _page++;
    [self requestWorkerDailyList];
}

- (void)requestWorkerDailyList
{
    WEAKSELF;
    NSDictionary *dic = @{@"page":[NSString stringWithFormat:@"%ld",_page]};
    [HYBaseHandler requestWorkerDailyParmers:dic Success:^(id obj) {
        [weakSelf.dailyProjectTableView.mj_header endRefreshing];
        [weakSelf.dailyProjectTableView.mj_footer endRefreshing];
        if ([obj server_Getcodecheck])
        {
            NSArray *dailyArray = [projectModel mj_objectArrayWithKeyValuesArray:obj[@"data"][@"content"]];
            if (_page == 0)
            {
                [weakSelf.dailyAry removeAllObjects];
            }
            [weakSelf.dailyAry addObjectsFromArray:dailyArray];
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf.dailyProjectTableView reloadData];
        });
    } failed:^(NSError *error) {
        [weakSelf.dailyProjectTableView.mj_header endRefreshing];
        [weakSelf.dailyProjectTableView.mj_footer endRefreshing];
    }];
}

// MARK:tableview的方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dailyAry.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 85*displaywidth;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.000001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.000001;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellPath];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (_dailyAry.count > 0)
    {
        projectModel *model = _dailyAry[indexPath.row];
        UILabel *projectNameLab = [ZCControl createLabelWithFrame:CGRectMake(15*displaywidth, 5*displaywidth, SCREEN_WIDTH-165*displaywidth, 25*displaywidth) Font:16.0 Text:model.projectname Color:ThemeColor];
        [cell.contentView addSubview:projectNameLab];
        NSString *timeStr = model.createDtm;
        if (timeStr.length > 15)
        {
            timeStr = [timeStr substringToIndex:model.createDtm.length-3];
        }
        
        UILabel *timeLab = [ZCControl createLabelWithFrame:CGRectMake(SCREEN_WIDTH-180*displaywidth, 5*displaywidth, 165*displaywidth, 25*displaywidth) Font:12.0 Text:timeStr Color:RGB(153, 153, 153)];
        timeLab.textAlignment = NSTextAlignmentRight;
        [cell.contentView addSubview:timeLab];
        
        
        UILabel *nameLab = [ZCControl createLabelWithFrame:CGRectMake(15*displaywidth, 30*displaywidth, SCREEN_WIDTH-30*displaywidth, 25*displaywidth) Font:13.0 Text:[NSString stringWithFormat:@"汇报人员：%@",model.user.name] Color:RGB(77, 77, 77)];
        [cell.contentView addSubview:nameLab];
        
        UILabel *contenLab = [ZCControl createLabelWithFrame:CGRectMake(15*displaywidth, 55*displaywidth, SCREEN_WIDTH-75*displaywidth, 25*displaywidth) Font:13.0 Text:[NSString stringWithFormat:@"汇报内容：%@",model.content] Color:RGB(77, 77, 77)];
        contenLab.lineBreakMode = NSLineBreakByTruncatingTail;
        contenLab.adjustsFontSizeToFitWidth = NO;
        [cell.contentView addSubview:contenLab];
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 84*displaywidth, SCREEN_WIDTH, 1*displaywidth)];
        lineView.backgroundColor = LinesColor;
        [cell.contentView addSubview:lineView];
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    projectModel *model = _dailyAry[indexPath.row];
    DailyDetailsViewController *vc = [[DailyDetailsViewController alloc] init];
    vc.projectModel = model;
    [self.navigationController pushViewController:vc animated:YES];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
